package com.lwgk.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.lwgk.AppComplication;
import com.lwgk.constant.ConstantUtils;
import com.lwgk.encryption.DesSecreat;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/12
 * --------------------------------------------------------------------------
 * 描述：主要是用于sp保存数据
 * 注意事项：不需要上下文
 */
public class SaveDataUtils {
    private static SharedPreferences sharedPreferences;

    /**
     * 保存数据到SharedPreferences
     *
     * @param key   键
     * @param value 需要保存的数据
     * @return 保存结果
     */
    public static boolean putData(String key, Object value) {
        boolean result;
        if (sharedPreferences == null)
            sharedPreferences = AppComplication.mContext.getSharedPreferences(ConstantUtils.USERDATASP, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String type = value.getClass().getSimpleName();
        try {
            switch (type) {
                case "Boolean":
                    editor.putBoolean(key, (Boolean) value);
                    break;
                case "Long":
                    editor.putLong(key, (Long) value);
                    break;
                case "Float":
                    editor.putFloat(key, (Float) value);
                    break;
                case "String":
                    //保存的时候加密
                    String secreatValue = (String) value;
                    editor.putString(key, DesSecreat.getInstance().encryptPassword(secreatValue));
                    break;
                case "Integer":
                    editor.putInt(key, (Integer) value);
                    break;
                default:
                    Gson gson = new Gson();
                    String json = gson.toJson(value);
                    editor.putString(key, json);
                    break;
            }
            result = true;
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        editor.apply();
        return result;
    }

    /**
     * 获取SharedPreferences中保存的数据
     *
     * @param key          键
     * @param defaultValue 获取失败默认值
     * @return 从SharedPreferences读取的数据
     */
    public static Object getData(String key, Object defaultValue) {
        Object result;
        if (sharedPreferences == null)
            sharedPreferences = AppComplication.mContext.getSharedPreferences(ConstantUtils.USERDATASP, Context.MODE_PRIVATE);
        String type = defaultValue.getClass().getSimpleName();
        try {
            switch (type) {
                case "Boolean":
                    result = sharedPreferences.getBoolean(key, (Boolean) defaultValue);
                    break;
                case "Long":
                    result = sharedPreferences.getLong(key, (Long) defaultValue);
                    break;
                case "Float":
                    result = sharedPreferences.getFloat(key, (Float) defaultValue);
                    break;
                case "String":
                    //取出的时候解密
                    String secreatValue = sharedPreferences.getString(key, (String) defaultValue);
                    result = DesSecreat.getInstance().decryptPassword(secreatValue);
                    break;
                case "Integer":
                    result = sharedPreferences.getInt(key, (Integer) defaultValue);
                    break;
                default:
                    Gson gson = new Gson();
                    String json = sharedPreferences.getString(key, "");
                    if (!json.equals("") && json.length() > 0) {
                        result = gson.fromJson(json, defaultValue.getClass());
                    } else {
                        result = defaultValue;
                    }
                    break;
            }
        } catch (Exception e) {
            result = null;
            e.printStackTrace();
        }
        return result;
    }
}
